package com.putao.album.user;

import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.putao.album.R;
import com.putao.album.base.BaseActivity;
import com.putao.album.eventbus.BasePostEvent;
import com.putao.album.user.util.FlowType;
import com.putao.album.util.ActivityHelper;
import com.putao.album.util.HttpRequestUtil;
import com.putao.album.util.Loger;
import com.putao.album.util.PuTaoConstants;
import com.putao.album.util.StringHelper;

import java.util.HashMap;

import de.greenrobot.event.EventBus;

/**
 * Created by jidongdong on 15/4/21.
 */
public class ActivityLoginRegister extends BaseActivity {
    private Button btn_next, btn_delete_tel;
    private TextView tv_protocal;
    private EditText etx_mobile;
    private String telnum;

    @Override
    public int doGetContentViewId() {
        return R.layout.activity_login_register;
    }

    @Override
    public void doInitSubViews(View view) {
        btn_next = queryViewById(R.id.btn_next);
        tv_protocal = queryViewById(R.id.tv_protocal);
        etx_mobile = queryViewById(R.id.etx_mobile);
        btn_delete_tel = queryViewById(R.id.btn_delete_tel);
        addOnClickListener(btn_next, tv_protocal, btn_delete_tel);
        tv_protocal.setText(Html.fromHtml(getResources().getString(R.string.btn_protocol_text)));
        etx_mobile.addTextChangedListener(mTelTextWatch);
        EventBus.getDefault().register(this);
    }

    TextWatcher mTelTextWatch = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String str = etx_mobile.getText().toString();
            if (!StringHelper.isEmpty(str)) {
                btn_next.setEnabled(true);
                btn_next.setAlpha(1f);
                btn_delete_tel.setVisibility(View.VISIBLE);
            } else {
                btn_next.setEnabled(false);
                btn_next.setAlpha(.5f);
                btn_delete_tel.setVisibility(View.GONE);
            }
        }
    };

    @Override
    public void doInitData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                checkMobileRegistered();
                break;
            case R.id.tv_protocal:
                ActivityHelper.startActivity(mActivity, ActivityUserProtocal.class);
                break;
            case R.id.btn_delete_tel:
                etx_mobile.setText("");
                break;

        }
    }

    /**
     * 检测手机号是否已经注册
     */
    void checkMobileRegistered() {
        telnum = etx_mobile.getText().toString();
        if (StringHelper.isEmpty(telnum)) {
            showToast("请输入手机号");
            return;
        }
        if (!StringHelper.checkMobileFormat(telnum)) {
            showToast("这不是手机号码哦，再试一下吧");
            return;
        }
        btn_next.setEnabled(false);
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("mobile", telnum);
        HttpRequestUtil.doHttpPostRequest(mContext, PuTaoConstants.API_CHECK_MOBILE, new RequestParams(HttpRequestUtil.buildHttpRequestParams(params)), true, new HttpRequestUtil.RequestResponseListener() {
            @Override
            public void success(int statuscode, String response) {
                if (!StringHelper.isEmpty(response)) {
                    Bundle bundle = new Bundle();
                    bundle.putString(FlowType.flag_mobile, telnum);
                    if (HttpRequestUtil.getResponseCode(response).equals(HttpRequestUtil.ResponseCode.api_code_200)) {
                        bundle.putInt(FlowType.flag_flowtype, FlowType.flow_register);
                        ActivityHelper.startActivity(mActivity, ActivityInputVCode.class, bundle);
                    } else {
                        bundle.putInt(FlowType.flag_flowtype, FlowType.flow_login);
                        ActivityHelper.startActivity(mActivity, ActivityInputPassword.class, bundle);
                    }
                }
                btn_next.setEnabled(true);
            }

            @Override
            public void failed(int statuscode, String response) {
                Loger.d("response:::" + response);
                showToast(response);
                btn_next.setEnabled(true);
            }
        });
    }


    public void onEvent(BasePostEvent event) {
        switch (event.eventCode) {
            case PuTaoConstants.EVENTBUS_LOGIN_SUCCESS:
                finish();
                break;
        }
    }


}
